import time, datetime


class StringDemo:

    def str_fun():
        # 修改字符串，其中中文索引需要转换成ascii的4位
        # r将特殊字符当普通字符
        name = "唐嫣"
        print name[:3]+"小鲮"
        print "\v"+name
        print r"\v"

        str = "Lily"
        print max(str)
        print str.swapcase()  # 翻转大小写

    @staticmethod
    def format__():
        print("Hallo,%s %s" % ("小米", "~~"))
        print("Hallo,%.1f %s" % (1.23456, 1.12))
        print("Hallo,%%s %s" % "1个")

    @staticmethod
    def center__():
        word = "我是单测"
        print(word.center(20))
        print(word.center(20, "*"))
        print(word.ljust(20, "*"))
        print(word.rjust(20, "*"))
        print("%30s" % word)

    @staticmethod
    def sub__():
        word = "hell world"
        print(word.startswith("he"))
        print(word.startswith("ll", 2))
        print(word.startswith("ll", 2, 3))

    @staticmethod
    def reverse__():
        s = "hello world"
        li = list(s)
        li.reverse()
        s = "".join(li)
        print(s)

    @staticmethod
    def replace__():
        s = "hello world"
        print(s.replace("l", "*", 2))

    @staticmethod
    def date2str():
        date_str = "2018-08-16"
        formater = "%Y-%m-%d"
        print(time.strftime(formater, time.localtime()))
        t = time.strptime(date_str, formater)
        print(t)
        y, m, d = t[0:3]
        print(datetime.datetime(y, m, d))

    @staticmethod
    def compare__():
        a = "我"
        b = "我"
        c = a
        assert c is a  # is判断是否属于同一对象
        assert a is b  # TODO 为什么这里也通过了
        assert a == b   # == 判断值是否相等
        assert a == "3"


if __name__ == '__main__':
    StringDemo.format__()
    StringDemo.center__()
    StringDemo.sub__()
    StringDemo.reverse__()
    StringDemo.replace__()
    StringDemo.date2str()
    StringDemo.compare__()
